#include <game.h>

chap::chap() : base("chap")
{
	listItem = new string<item*>;
}

chap::~chap()
{
	delete listItem;
}

int chap::init(int i,coeur* c,IXMLReader* xmlChap)
{
	base::init(i,c);

	int l=0;

	log(stringc("Chap xml : ") + i + " loading ...");
	while(xmlChap && xmlChap->read())
	{
		if (xmlChap->getNodeType()==EXN_ELEMENT)
		{
			if (core::stringw("item") == xmlChap->getNodeName())
			{
				addItem(
					xmlChap->getAttributeValueAsInt(L"id"),
					mycore->loadTexture(xmlChap->getAttributeValue(L"texture")),
					xmlChap->getAttributeValueAsInt(L"combi"),
					xmlChap->getAttributeValueAsInt(L"combiResult")
				);
				log( stringc("New item : id:") + xmlChap->getAttributeValueAsInt(L"id") + ", texture: " + xmlChap->getAttributeValue(L"texture") + ", combi: " + xmlChap->getAttributeValueAsInt(L"combi") + ", combiResult: " + xmlChap->getAttributeValueAsInt(L"combiResult"));
			}
			else if (core::stringw("lieu") == xmlChap->getNodeName())
			{
				l = xmlChap->getAttributeValueAsInt(L"id");
				log(stringc("Chosen lieu is ") + l);
			}
		}
	}
	return l;
}

int chap::update()
{
	return 1;
}

int chap::close()
{
	log("Closing each item :");
	while (listItem->size()>0)
	{
		log(stringc("Deleting item ") + (*listItem)[0]->getId());
		delete (*listItem)[0];
		listItem->erase(0);
	}
	base::close();
	return 1;
}

int chap::addItem(int i,ITexture* tex,int it,int cr)
{
	item *m = new item;
	m->init(i,tex,it,cr);
	*listItem += m;
	log(stringc("item ") + i + "added");
	return 1;
}

item* chap::getItemFromId(int id)
{
	for (unsigned int i=0;i<listItem->size();i++)
		if ((*listItem)[i]->getId() == id)
		{
			return (*listItem)[i];
		}
	return 0;
}

bool chap::OnEvent(const SEvent& event)
{
	return false;
}

